#include "vm.h"
#include "world_utils.h"
#include <stdlib.h>
#include <time.h>

/*
 * Fills a given world with blank dots.
 */
void fill_with_blanks(dot* world, int world_size) {
	int i;
	for (i = 0; i < world_size; i++) {
		clear_dot(world + i);
	}
}

/*
 * Puts food in random places in the world.
 */
void fill_with_food(dot* world, int world_size, int amount) {
	int i;
	for (i = 0; i < amount; i++) {
		put_food(&(world[random(world_size)]));
	}
}

/*
 * Puts AI in random places in the world.
 */
void fill_with_ai(dot* world, int world_size, int amount, short* commands, int com_amount) {
	int i;
	for (i = 0; i < amount; i++) {
		put_new_ai(&(world[random(world_size)]), commands, com_amount);
	}
}



/*
 * Gets a random number between 0 and max, with time as seed.
 */
int random(int max) {
	
	return (rand() % max) + 1;
}

#define WORLD_WIDTH 10
#define WORLD_HEIGHT 10
#define WORLD_SIZE (WORLD_WIDTH * WORLD_HEIGHT)
#define FOOD_AMOUNT 5
#define TURNS 5

int main() {
	int i;
	char file_num[10];
	char* bmp_ext = ".bmp";
	char full_file_name[20] ;
	char* base_file = "C:\\dots\\dots";
	short coms[1] = { COM_MOV | RIGHT };
	dot world[WORLD_SIZE];
	srand(time(NULL));
	fill_with_blanks(world, WORLD_SIZE);
	fill_with_food(world, WORLD_SIZE, FOOD_AMOUNT);
	fill_with_ai(world, WORLD_SIZE, 2, &coms[0], 1);

	for (i = 0; i < TURNS; i++) {
		// Make file name
		strcpy(full_file_name, base_file);
		itoa(i, file_num, 10);
		strcat(full_file_name, file_num);
		strcat(full_file_name, bmp_ext);

		write_bmp(full_file_name, WORLD_WIDTH, WORLD_HEIGHT, &world[0]);
		run_world(world, WORLD_HEIGHT);
	}

	write_bmp("C:\\dots\\dots_final.bmp", WORLD_WIDTH, WORLD_HEIGHT, &world[0]);
	free_all_ai_data(world, WORLD_SIZE);
	return 0;
}